package {
	import com.googlecode.milib.data.value.MLNumberValue;	
	
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	
	import com.googlecode.milib.animation.static_.Animation;
	import com.googlecode.milib.animation.static_.MLEase;
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBaseClass;
	import com.googlecode.milib.dbg.MLDBG;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.service.singleton.KeyboardService;
	import com.googlecode.milib.vo.MLEventInfo;
	import com.googlecode.milib.animation.static_.AnimationElement;		

	/**
	 * @author Marek Brun 'minim'
	 */
	public class AandBStatic extends MLBaseClass implements IMLBroadcasterListener {

		private var mlmc:MLMC;
		private var mc_ball:MovieClip;
		private var mc_a:MovieClip;
		private var mc_b:MovieClip;
		private var animToA:Animation;
		private var animToB:Animation;
		private var ks:KeyboardService;
		private var easeToA:MLEase;
		private var timeToA:MLNumberValue;
		private var animToB_y:AnimationElement;

		public function AandBStatic(mc:MovieClip) {
			mlmc=MLMC.forInstance(mc);
			
			mc_ball=mlmc.getMC('ball');
			mc_a=mlmc.getMC('a');
			mc_b=mlmc.getMC('b');
						easeToA=MLEase.getInstanceForName('ToA');
			
			//easeToA.setupEaseBezier(-1, 1); //you can manually setup eases
			
			timeToA=MLNumberValue.getInstanceForID('ToA');
			
			animToA=new Animation();
			animToA.setupWorkingTimeAsDynamicNumber(timeToA);
			animToA.addElement(mc_ball, 'x', mc_a.x, easeToA);
			animToA.addElement(mc_ball, 'y', mc_a.y, easeToA);
			
			//if you wanna use dynamic values then this is faster way
			//see in main MLDBG window > numbers and eases
			//paste changed values in main class
			animToB=Animation.createNewDynamicForID('ToB');
			animToB.addElement(mc_ball, 'x', mc_b.x);
			
			//if you wanna manipuate later in one element, just get it when adding
			animToB_y=animToB.addElement(mc_ball, 'y', 100000000);
			
			//in most cases it is target value
			//you can change it in any time
			animToB_y.target=mc_b.y;
			
			animToA.addListener(this);
			animToB.addListener(this);
			
			ks=KeyboardService.getInstance();
			ks.addListener(this);
		}
		
		public function get mc():MovieClip { return mlmc.mc; };
		
//********************************************************************************************
//		events for AandB
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case ks:
					switch(ev.event) {
						case ks.event_Down:
							switch(KeyboardEvent(ev.data).keyCode) {
								case KeyboardService.KEY_A:
									MLDBG.log('A');
									animToA.start();
								break;
								case KeyboardService.KEY_B:
									MLDBG.log('B');
									animToB.start();
								break;
							}
						break;
					}
				break;
				case animToA:
					switch(ev.event) {
						case animToA.event_ActionStart:
							MLDBG.log('animToA event '+ev.event);
						break;
						case animToA.event_ActionFinish:
							MLDBG.log('animToA event '+ev.event);
						break;
					}
				break;
				case animToB:
					switch(ev.event) {
						case animToB.event_ActionStart:
							MLDBG.log('animToB event '+ev.event);
						break;
						case animToB.event_ActionFinish:
							MLDBG.log('animToB event '+ev.event);
						break;
					}
				break;
			}
		}
		
		
	}
}
